
clear;
close all;
PsychHID('Version');
KbQueueCreate();
oldLevel = Screen('Preference', 'VisualDebugLevel', 1); %level 1: error only
oldEnableFlag = Screen('Preference', 'SuppressAllWarnings', 1);
Screen('Preference', 'SkipSyncTests', 1);
[keyPress, keyCodes]= Kb_bcoe();

%cd C:\_bcoe\EDF_Data\;
%cd('C:\bcoe\crap')
c=clock;
day=[num2str(c(1)) '-' num2str(c(2)) '-' num2str(c(3)) ];
if day(7)=='-'
    day=[day(1:5), '0',day(6:end)];
end
if length(day)==9;
    day=[day(1:8), '0',day(9:end)];
end
warning off MATLAB:MKDIR:DirectoryExists
eval(['mkdir ' day]);
warning on MATLAB:MKDIR:DirectoryExists
eval(['cd ' day]);

c=define_colors(uint8(256));
c.BACKGROUND=c.M_GREY;

%c.BLUE=c.BLUE+uint8([.3 .3 0]*256);
scnsize = get(0,'ScreenSize');
screenpixH=scnsize(3);
screenpixW=scnsize(4);
c_x=screenpixH/2;
c_y=screenpixW/2;
% for now....
dist2screen=60;%cm
screenwidth=40;%cm
dphs = atan2(screenwidth/2,dist2screen)*180/pi;%degrees per half screen
ppd = (screenpixW)/2/dphs; %pixels/degree

fix=round([c_x c_y c_x c_y]+([-1 -1	1 1]*ppd*.5));
stim=round(([-1 -1	1 1]*ppd*3));
loc_AngEcc=	[ 0 12; 60 12; 120 12; 180 12; 240 12; 300 12];    % ang & ecc positions. only change this line!!!
n_loc=length(loc_AngEcc);
loc_XY=[];
[loc_XY(:,1) loc_XY(:,2)]=polar2cart(loc_AngEcc(:,1),loc_AngEcc(:,2),1);
[loc_XYm(:,1) loc_XYm(:,2)]=polar2cart(loc_AngEcc(:,1),loc_AngEcc(:,2)+.6,1);  % the +.6 is for the width of the targtes which may have to be changed
pixloc_XY=[];
pixloc_XY(:,1)=(loc_XY(:,1)*ppd)+c_x;% set ang&ecc to x&y based on screen size
pixloc_XY(:,2)=(loc_XY(:,2)*ppd)+c_y;
for i=1:n_loc
    tgt_rect(i,:)=round([pixloc_XY(i,:) pixloc_XY(i,:)]+stim); % for use with mac's LeToRiBo rect shite
end
jj=1;
try
    [w, wRect]=Screen('OpenWindow', 2);
    SCREEN(w,'fillrect',c.BACKGROUND);
    setmouse(scnsize(3),scnsize(4)); % force mouse to edge
    [m_x m_y buttons]=getmouse;
    m_y(1:6)=m_y;
    buttons=0;
    toggle=1;
    KbQueueStart();
    
    while buttons(1)==0
        SCREEN(w,'filloval',c.BLACK,fix)
        if toggle
            SCREEN(w,'filloval',round(c.D_GREY  *(screenpixH/screenpixH)),tgt_rect(1,:))
            SCREEN(w,'filloval',round(c.L_GREY  *(screenpixH/screenpixH)),tgt_rect(2,:))
            SCREEN(w,'filloval',round(c.D_GREY  *(screenpixH/screenpixH)),tgt_rect(3,:))
            SCREEN(w,'filloval',round(c.L_GREY  *(screenpixH/screenpixH)),tgt_rect(4,:))
            SCREEN(w,'filloval',round(c.D_GREY  *(screenpixH/screenpixH)),tgt_rect(5,:))
            SCREEN(w,'filloval',round(c.L_GREY  *(screenpixH/screenpixH)),tgt_rect(6,:))
        else
            SCREEN(w,'filloval',round(c.BLUE   *(m_y(1)/screenpixW)),tgt_rect(1,:))
            SCREEN(w,'filloval',round(c.GREEN  *(m_y(2)/screenpixW)),tgt_rect(2,:))
            SCREEN(w,'filloval',round(c.RED    *(m_y(3)/screenpixW)),tgt_rect(3,:))
            SCREEN(w,'filloval',round(c.YELLOW *(m_y(4)/screenpixW)),tgt_rect(4,:))
            SCREEN(w,'filloval',round(c.ORANGE *(m_y(5)/screenpixW)),tgt_rect(5,:))
            SCREEN(w,'filloval',round(c.L_GREY *(m_y(6)/screenpixW)),tgt_rect(6,:))
        end
        toggle=abs(toggle-1);
        SCREEN('flip',w);
        [keyPress, keyCodes]= Kb_bcoe();
        if keyPress
            switch keyCodes(1)
                case 49 %'1'
                    jj=1;
                case 50 %'2'
                    jj=2;
                case 51 %'3
                    jj=3;
                case 52 %'4'
                    jj=4;
                case 53 %'5'
                    jj=5;
                case 54 %'6'
                    jj=6;
                case 27%'esc'
                    buttons=1;
                otherwise
                    keyCodes(1)
            end
        end
        [m_x m_y(jj) buttons]=getmouse;
        
    end
    SCREEN(w,'Close');
    %disp(' ');
    %disp(['set M_GREY to ' num2str(round(screenpixH/screenpixH*1000)/10) '%']);
    %disp(['set COLOR to ' num2str(round(m_y/screenpixW*1000)/10) '%']);
    figure(1);clf;
    ylim([0 2])
    vfill(0,6,double(c.BACKGROUND)/256);
    ylim([.5 1.5])
    vfill(1,2,double(c.BLUE  )/256*(m_y(1)/screenpixW));
    vfill(2,3,double(c.GREEN )/256*(m_y(2)/screenpixW));
    vfill(3,4,double(c.RED   )/256*(m_y(3)/screenpixW));
    vfill(4,5,double(c.YELLOW)/256*(m_y(4)/screenpixW));
catch badness
    badness.message
    badness.stack
    shit
    clear mex

end
KbQueueStop;
KbQueueRelease;
clear mex
